package com.jeeplus.config;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class CasConfig {

    @Value("${cas.server-url-prefix}")
    private String casServer;

    @Value("${cas.service}")
    private String serviceUrl;

    @Value("${cas.clientUrl}")
    private String clientUrl;

    /**
     * CAS认证过滤器 - 对应 CASFilter
     */
    @Bean
    public FilterRegistrationBean<AuthenticationFilterNew> casAuthenticationFilter() {
        FilterRegistrationBean<AuthenticationFilterNew> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new AuthenticationFilterNew());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);

        Map<String, String> initParameters = new HashMap<>();
        initParameters.put("casServerLoginUrl", casServer+"/login");
        initParameters.put("serverName", serviceUrl);
        initParameters.put("clientUrl", clientUrl);
        registrationBean.setInitParameters(initParameters);

        return registrationBean;
    }

    /**
     * CAS票据验证过滤器 - 对应 CAS Validation Filter
     */
//    @Bean
  //  public FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> casValidationFilter() {
//        FilterRegistrationBean<Cas20ProxyReceivingTicketValidationFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(new Cas20ProxyReceivingTicketValidationFilter());
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
//
//        Map<String, String> initParameters = new HashMap<>();
//        initParameters.put("casServerUrlPrefix", "http://authserver.jlnu.edu.cn/authserver");
//        initParameters.put("serverName", "http://192.168.0.13:8082");
//        initParameters.put("encoding", "UTF-8");
//        registrationBean.setInitParameters(initParameters);
//
//        return registrationBean;
//    }

    /**
     * HttpServletRequest包装过滤器 - 对应 CAS HttpServletRequest Wrapper Filter
     */
//    @Bean
//    public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilter() {
//        FilterRegistrationBean<HttpServletRequestWrapperFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(new HttpServletRequestWrapperFilter());
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE + 2);
//
//        return registrationBean;
//    }
}